<?php

class UserAction extends CommonAction{
public function __construct()
{
parent::__construct();
require_once APP_ROOT_PATH.'/system/libs/user.php';
}
public function index()
{
$group_list = M('UserGroup')->findAll();
$this->assign('group_list',$group_list);
$map[DB_PREFIX.'user.is_delete'] = 0;
if(intval($_REQUEST['group_id'])>0)
{
$map[DB_PREFIX.'user.group_id'] = intval($_REQUEST['group_id']);
}
if(trim($_REQUEST['user_name'])!='')
{
$map[DB_PREFIX.'user.user_name'] = array('like','%'.trim($_REQUEST['user_name']).'%');
}
if(trim($_REQUEST['email'])!='')
{
$map[DB_PREFIX.'user.email'] = array('like','%'.trim($_REQUEST['email']).'%');
}
if(trim($_REQUEST['mobile'])!='')
{
$map[DB_PREFIX.'user.mobile'] = array('like','%'.trim($_REQUEST['mobile']).'%');
}
if(trim($_REQUEST['pid_name'])!='')
{
$pid = M('User')->where("user_name='".trim($_REQUEST['pid_name'])."'")->getField('id');
$map[DB_PREFIX.'user.pid'] = $pid;
}
if (method_exists ( $this,'_filter')) {
$this->_filter ( $map );
}
$name=$this->getActionName();
$model = D ($name);
if (!empty ( $model )) {
$this->_list ( $model,$map );
}
$this->display ();
}
public function trash()
{
$condition['is_delete'] = 1;
$this->assign('default_map',$condition);
parent::index();
}
public function add()
{
$group_list = M('UserGroup')->findAll();
$this->assign('group_list',$group_list);
$cate_list = M('TopicTagCate')->findAll();
$this->assign('cate_list',$cate_list);
$field_list = M('UserField')->order('sort desc')->findAll();
foreach($field_list as $k=>$v)
{
$field_list[$k]['value_scope'] = preg_split('/[ ,]/i',$v['value_scope']);
}
$this->assign('field_list',$field_list);
$this->display();
}
public function insert() {
B('FilterString');
$ajax = intval($_REQUEST['ajax']);
$data = M(MODULE_NAME)->create ();
$this->assign('jumpUrl',u(MODULE_NAME.'/add'));
if(!check_empty($data['user_pwd']))
{
$this->error(L('USER_PWD_EMPTY_TIP'));
}
if($data['user_pwd']!=$_REQUEST['user_confirm_pwd'])
{
$this->error(L('USER_PWD_CONFIRM_ERROR'));
}
$res = save_user($_REQUEST);
if($res['status']==0)
{
$error_field = $res['data'];
if($error_field['error'] == EMPTY_ERROR)
{
if($error_field['field_name'] == 'user_name')
{
$this->error(L('USER_NAME_EMPTY_TIP'));
}
elseif($error_field['field_name'] == 'email')
{
$this->error(L('USER_EMAIL_EMPTY_TIP'));
}
else
{
$this->error(sprintf(L('USER_EMPTY_ERROR'),$error_field['field_show_name']));
}
}
if($error_field['error'] == FORMAT_ERROR)
{
if($error_field['field_name'] == 'email')
{
$this->error(L('USER_EMAIL_FORMAT_TIP'));
}
if($error_field['field_name'] == 'mobile')
{
$this->error(L('USER_MOBILE_FORMAT_TIP'));
}
}
if($error_field['error'] == EXIST_ERROR)
{
if($error_field['field_name'] == 'user_name')
{
$this->error(L('USER_NAME_EXIST_TIP'));
}
if($error_field['field_name'] == 'email')
{
$this->error(L('USER_EMAIL_EXIST_TIP'));
}
}
}
$user_id = intval($res['user_id']);
foreach($_REQUEST['auth'] as $k=>$v)
{
foreach($v as $item)
{
$auth_data = array();
$auth_data['m_name'] = $k;
$auth_data['a_name'] = $item;
$auth_data['user_id'] = $user_id;
M('UserAuth')->add($auth_data);
}
}
foreach($_REQUEST['cate_id'] as $cate_id)
{
$link_data = array();
$link_data['user_id'] = $user_id;
$link_data['cate_id'] = $cate_id;
M('UserCateLink')->add($link_data);
}
$log_info = $data['user_name'];
save_log($log_info.L('INSERT_SUCCESS'),1);
$this->success(L('INSERT_SUCCESS'));
}
public function edit() {
$id = intval($_REQUEST ['id']);
$condition['is_delete'] = 0;
$condition['id'] = $id;
$vo = M(MODULE_NAME)->where($condition)->find();
$this->assign ( 'vo',$vo );
$group_list = M('UserGroup')->findAll();
$this->assign('group_list',$group_list);
$cate_list = M('TopicTagCate')->findAll();
foreach($cate_list as $k=>$v)
{
$cate_list[$k]['checked'] = M('UserCateLink')->where('user_id='.$vo['id'].' and cate_id = '.$v['id'])->count();
}
$this->assign('cate_list',$cate_list);
$field_list = M('UserField')->order('sort desc')->findAll();
foreach($field_list as $k=>$v)
{
$field_list[$k]['value_scope'] = preg_split('/[ ,]/i',$v['value_scope']);
$field_list[$k]['value'] = M('UserExtend')->where('user_id='.$id.' and field_id='.$v['id'])->getField('value');
}
$this->assign('field_list',$field_list);
$rs = M('UserAuth')->where('user_id='.$id.' and rel_id = 0')->findAll();
foreach($rs as $row)
{
$auth_list[$row['m_name']][$row['a_name']] = 1;
}
$this->assign('auth_list',$auth_list);
$this->display ();
}
public function delete() {
$ajax = intval($_REQUEST['ajax']);
$id = $_REQUEST ['id'];
if (isset ( $id )) {
if(M('DealOrder')->where(array ('user_id'=>array ('in',explode ( ',',$id ) ) ))->count()>0)
{
$this->error (l('ORDER_EXIST_DELETE_FAILED'),$ajax);
}
$condition = array ('id'=>array ('in',explode ( ',',$id ) ) );
$rel_data = M(MODULE_NAME)->where($condition)->findAll();
foreach($rel_data as $data)
{
$info[] = $data['user_name'];
}
if($info) $info = implode(',',$info);
$list = M(MODULE_NAME)->where ( $condition )->setField ( 'is_delete',1 );
if ($list!==false) {
M('Topic')->where('user_id in ('.$id.')')->setField('is_effect',0);
M('TopicReply')->where('user_id in ('.$id.')')->setField('is_effect',0);
M('Message')->where('user_id in ('.$id.')')->setField('is_effect',0);
save_log($info.l('DELETE_SUCCESS'),1);
$this->success (l('DELETE_SUCCESS'),$ajax);
}else {
save_log($info.l('DELETE_FAILED'),0);
$this->error (l('DELETE_FAILED'),$ajax);
}
}else {
$this->error (l('INVALID_OPERATION'),$ajax);
}
}
public function restore() {
$ajax = intval($_REQUEST['ajax']);
$id = $_REQUEST ['id'];
if (isset ( $id )) {
$condition = array ('id'=>array ('in',explode ( ',',$id ) ) );
$rel_data = M(MODULE_NAME)->where($condition)->findAll();
foreach($rel_data as $data)
{
$info[] = $data['user_name'];
}
if($info) $info = implode(',',$info);
$list = M(MODULE_NAME)->where ( $condition )->setField ( 'is_delete',0 );
if ($list!==false) {
M('Topic')->where('user_id in ('.$id.')')->setField('is_effect',1);
M('TopicReply')->where('user_id in ('.$id.')')->setField('is_effect',1);
M('Message')->where('user_id in ('.$id.')')->setField('is_effect',1);
save_log($info.l('RESTORE_SUCCESS'),1);
$this->success (l('RESTORE_SUCCESS'),$ajax);
}else {
save_log($info.l('RESTORE_FAILED'),0);
$this->error (l('RESTORE_FAILED'),$ajax);
}
}else {
$this->error (l('INVALID_OPERATION'),$ajax);
}
}
public function foreverdelete() {
$ajax = intval($_REQUEST['ajax']);
$id = $_REQUEST ['id'];
if (isset ( $id )) {
$condition = array ('id'=>array ('in',explode ( ',',$id ) ) );
$rel_data = M(MODULE_NAME)->where($condition)->findAll();
foreach($rel_data as $data)
{
$info[] = $data['user_name'];
}
if($info) $info = implode(',',$info);
$ids = explode ( ',',$id );
foreach($ids as $uid)
{
delete_user($uid);
}
save_log($info.l('FOREVER_DELETE_SUCCESS'),1);
clear_auto_cache('consignee_info');
$this->success (l('FOREVER_DELETE_SUCCESS'),$ajax);
}else {
$this->error (l('INVALID_OPERATION'),$ajax);
}
}
public function update() {
B('FilterString');
$data = M(MODULE_NAME)->create ();
$log_info = M(MODULE_NAME)->where('id='.intval($data['id']))->getField('user_name');
$this->assign('jumpUrl',u(MODULE_NAME.'/edit',array('id'=>$data['id'])));
if(!check_empty($data['user_pwd'])&&$data['user_pwd']!=$_REQUEST['user_confirm_pwd'])
{
$this->error(L('USER_PWD_CONFIRM_ERROR'));
}
$res = save_user($_REQUEST,'UPDATE');
if($res['status']==0)
{
$error_field = $res['data'];
if($error_field['error'] == EMPTY_ERROR)
{
if($error_field['field_name'] == 'user_name')
{
$this->error(L('USER_NAME_EMPTY_TIP'));
}
elseif($error_field['field_name'] == 'email')
{
$this->error(L('USER_EMAIL_EMPTY_TIP'));
}
else
{
$this->error(sprintf(L('USER_EMPTY_ERROR'),$error_field['field_show_name']));
}
}
if($error_field['error'] == FORMAT_ERROR)
{
if($error_field['field_name'] == 'email')
{
$this->error(L('USER_EMAIL_FORMAT_TIP'));
}
if($error_field['field_name'] == 'mobile')
{
$this->error(L('USER_MOBILE_FORMAT_TIP'));
}
}
if($error_field['error'] == EXIST_ERROR)
{
if($error_field['field_name'] == 'user_name')
{
$this->error(L('USER_NAME_EXIST_TIP'));
}
if($error_field['field_name'] == 'email')
{
$this->error(L('USER_EMAIL_EXIST_TIP'));
}
}
}
M('UserAuth')->where('user_id='.$data['id'].' and rel_id = 0')->delete();
foreach($_REQUEST['auth'] as $k=>$v)
{
foreach($v as $item)
{
$auth_data = array();
$auth_data['m_name'] = $k;
$auth_data['a_name'] = $item;
$auth_data['user_id'] = $data['id'];
M('UserAuth')->add($auth_data);
}
}
M('User')->where('id='.intval($_REQUEST['id']))->setField('is_effect',intval($_REQUEST['is_effect']));
$user_id = intval($_REQUEST['id']);
M('UserCateLink')->where('user_id='.$user_id)->delete();
foreach($_REQUEST['cate_id'] as $cate_id)
{
$link_data = array();
$link_data['user_id'] = $user_id;
$link_data['cate_id'] = $cate_id;
M('UserCateLink')->add($link_data);
}
save_log($log_info.L('UPDATE_SUCCESS'),1);
$this->success(L('UPDATE_SUCCESS'));
}
public function set_effect()
{
$id = intval($_REQUEST['id']);
$ajax = intval($_REQUEST['ajax']);
$info = M(MODULE_NAME)->where('id='.$id)->getField('user_name');
$c_is_effect = M(MODULE_NAME)->where('id='.$id)->getField('is_effect');
$n_is_effect = $c_is_effect == 0 ?1 : 0;
M(MODULE_NAME)->where('id='.$id)->setField('is_effect',$n_is_effect);
save_log($info.l('SET_EFFECT_'.$n_is_effect),1);
$this->ajaxReturn($n_is_effect,l('SET_EFFECT_'.$n_is_effect),1)	;
}
public function account()
{
$user_id = intval($_REQUEST['id']);
$user_info = M('User')->getById($user_id);
$this->assign('user_info',$user_info);
$this->display();
}
public function modify_account()
{
$user_id = intval($_REQUEST['id']);
$money = floatval($_REQUEST['money']);
$score = intval($_REQUEST['score']);
$point = intval($_REQUEST['point']);
$msg = trim($_REQUEST['msg'])==''?l('ADMIN_MODIFY_ACCOUNT'):trim($_REQUEST['msg']);
modify_account(array('money'=>$money,'score'=>$score,'point'=>$point),$user_id,$msg);
save_log(l('ADMIN_MODIFY_ACCOUNT'),1);
$this->success(L('UPDATE_SUCCESS'));
}
public function account_detail()
{
$user_id = intval($_REQUEST['id']);
$user_info = M('User')->getById($user_id);
$this->assign('user_info',$user_info);
$map['user_id'] = $user_id;
if (method_exists ( $this,'_filter')) {
$this->_filter ( $map );
}
$model = M ('UserLog');
if (!empty ( $model )) {
$this->_list ( $model,$map );
}
$this->display ();
return;
}
public function foreverdelete_account_detail()
{
$ajax = intval($_REQUEST['ajax']);
$id = $_REQUEST ['id'];
if (isset ( $id )) {
$condition = array ('id'=>array ('in',explode ( ',',$id ) ) );
$rel_data = M('UserLog')->where($condition)->findAll();
foreach($rel_data as $data)
{
$info[] = $data['id'];
}
if($info) $info = implode(',',$info);
$list = M('UserLog')->where ( $condition )->delete();
if ($list!==false) {
save_log($info.l('FOREVER_DELETE_SUCCESS'),1);
$this->success (l('FOREVER_DELETE_SUCCESS'),$ajax);
}else {
save_log($info.l('FOREVER_DELETE_FAILED'),0);
$this->error (l('FOREVER_DELETE_FAILED'),$ajax);
}
}else {
$this->error (l('INVALID_OPERATION'),$ajax);
}
}
public function export_csv($page = 1)
{
set_time_limit(0);
$limit = (($page -1)*intval(app_conf('BATCH_PAGE_SIZE'))).','.(intval(app_conf('BATCH_PAGE_SIZE')));
$map[DB_PREFIX.'user.is_delete'] = 0;
if(intval($_REQUEST['group_id'])>0)
{
$map[DB_PREFIX.'user.group_id'] = intval($_REQUEST['group_id']);
}
if(trim($_REQUEST['user_name'])!='')
{
$map[DB_PREFIX.'user.user_name'] = array('like','%'.trim($_REQUEST['user_name']).'%');
}
if(trim($_REQUEST['email'])!='')
{
$map[DB_PREFIX.'user.email'] = array('like','%'.trim($_REQUEST['email']).'%');
}
if(trim($_REQUEST['mobile'])!='')
{
$map[DB_PREFIX.'user.mobile'] = array('like','%'.trim($_REQUEST['mobile']).'%');
}
if(trim($_REQUEST['pid_name'])!='')
{
$pid = M('User')->where("user_name='".trim($_REQUEST['pid_name'])."'")->getField('id');
$map[DB_PREFIX.'user.pid'] = $pid;
}
$list = M(MODULE_NAME)
->where($map)
->join(DB_PREFIX.'user_group ON '.DB_PREFIX.'user.group_id = '.DB_PREFIX.'user_group.id')
->field(DB_PREFIX.'user.*,'.DB_PREFIX.'user_group.name')
->limit($limit)->findAll();
if($list)
{
register_shutdown_function(array(&$this,'export_csv'),$page+1);
$user_value = array('id'=>'""','user_name'=>'""','email'=>'""','mobile'=>'""','group_id'=>'""');
if($page == 1)
$content = iconv('utf-8','gbk','编号,用户名,电子邮箱,手机号,会员组');
$extend_fields = M('UserField')->order('sort desc')->findAll();
foreach($extend_fields as $k=>$v)
{
$user_value[$v['field_name']] = '""';
if($page==1)
$content = $content.','.iconv('utf-8','gbk',$v['field_show_name']);
}
if($page==1) 	
$content = $content ."\n";
foreach($list as $k=>$v)
{
$user_value = array();
$user_value['id'] = iconv('utf-8','gbk','"'.$v['id'] .'"');
$user_value['user_name'] = iconv('utf-8','gbk','"'.$v['user_name'] .'"');
$user_value['email'] = iconv('utf-8','gbk','"'.$v['email'] .'"');
$user_value['mobile'] = iconv('utf-8','gbk','"'.$v['mobile'] .'"');
$user_value['group_id'] = iconv('utf-8','gbk','"'.$v['name'] .'"');
$extend_fieldsval = M('UserExtend')->where('user_id='.$v['id'])->findAll();
foreach($extend_fields as $kk=>$vv)
{
foreach($extend_fieldsval as $kkk=>$vvv)
{
if($vv['id']==$vvv['field_id'])
{
$user_value[$vv['field_name']] = iconv('utf-8','gbk','"'.$vvv['value'].'"');
break;
}
}
}
$content .= implode(',',$user_value) ."\n";
}
header('Content-Disposition: attachment; filename=user_list.csv');
echo $content;
}
else
{
if($page==1)
$this->error(L('NO_RESULT'));
}
}
function check_merchant_name()
{
$merchant_name = addslashes(trim($_REQUEST['merchant_name']));
$ajax = intval($_REQUEST['ajax']);
$result = $GLOBALS['db']->getOne('select count(*) from '.DB_PREFIX."supplier_account where account_name = '".$merchant_name."'");
if(intval($result)==0)
$this->error(l('MERCHANT_NAME_NOT_EXIST'),$ajax);
else
$this->success('',$ajax);
}
}
?>